package com.elong.businesstravel.base.recorder;

import com.android.support.jhf.debug.DebugLog;
import com.elong.businesstravel.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class VUMeter extends BaseMeterImageView {
    static final long  ANIMATION_INTERVAL = 70;
    
    Paint mPaint, mShadow;
    float mCurrentAngle;
    
    Bitmap iconVoiceBitmap1 = null;
    Bitmap iconVoiceBitmap2 = null;
    Bitmap iconVoiceBitmap3 = null;
    Bitmap iconVoiceBitmap4 = null;
    Bitmap iconVoiceBitmap5 = null;

    public VUMeter(Context context) {
        super(context);
        init(context);
    }

    public VUMeter(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    void init(Context context) {
        Drawable background = context.getResources().getDrawable(R.drawable.bg_voice);
        setBackgroundDrawable(background);
        
        iconVoiceBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_voice1);
        iconVoiceBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_voice2);
        iconVoiceBitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_voice3);
        iconVoiceBitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_voice4);
        iconVoiceBitmap5 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_voice5);
        
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mShadow = new Paint(Paint.ANTI_ALIAS_FLAG);
        mShadow.setColor(Color.argb(60, 0, 0, 0));
        
        mRecorder = null;
        
        mCurrentAngle = 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//
        float w = getWidth();
        float pivotX = w/2;
        
        float x =(float) mRecorder.getMaxAmplitude()/32768; 
      
//        DebugLog.logE("X"+x);
        Bitmap bitmap;
        if(x>=0 && x<0.2) {
        	bitmap = iconVoiceBitmap1;
        } else if(x>=0.2 && x<0.4) {
        	bitmap = iconVoiceBitmap2;
        } else if(x>=0.4 && x<0.6) {
        	bitmap = iconVoiceBitmap3;
        } else if(x>=0.6 && x<0.8) {
        	bitmap = iconVoiceBitmap4;
        } else {
        	bitmap = iconVoiceBitmap5;
        }
        canvas.drawBitmap(bitmap, pivotX-bitmap.getWidth()/2, 0, mPaint);
        
        if (mRecorder != null && mRecorder.state() == Recorder.RECORDING_STATE)
        	postInvalidateDelayed(ANIMATION_INTERVAL);
    }
}
